import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

class 两数之和 {
    public static void main(String[] args) {
        int[] ints = twoSum(new int[]{2, 7, 11, 15}, 9);
        System.out.println(Arrays.toString(ints));
    }
    public static int[] twoSum(int[] nums, int target) {
        //思路：利用hashmap，相当于加上了记忆功能
        Map<Integer,Integer> hashMap=new HashMap<>();
        //key为值，value为下标
        for (int i = 0; i < nums.length; i++) {
            if(hashMap.containsKey(target-nums[i])){
                return new int[]{hashMap.get(target-nums[i]),i};
            }
             hashMap.put(nums[i],i);
        }
        return null;
    }
}